package com.bykj.taketurns.findfragment.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bykj.taketurns.R;
import com.bykj.taketurns.findfragment.bean.ThreeTextBean;

import java.util.List;


class ThreeTextItemAdapter extends RecyclerView.Adapter<ThreeTextItemAdapter.ViewHolder> implements View.OnClickListener {

    private Context context;
    private List<ThreeTextBean.StoryCharacterBean.StoryCharacterSetBean> list;

    private OnItemClickListener mOnItemClickListener = null;

    public ThreeTextItemAdapter(Context context, List<ThreeTextBean.StoryCharacterBean.StoryCharacterSetBean> storyCharacterSet) {

        this.context = context;
        this.list = storyCharacterSet;
    }

    @Override
    public void onClick(View view) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(view, (Integer) view.getTag());
        }
    }

    void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.item_script_rv_text, parent, false);

        ViewHolder viewHolder = new ViewHolder(view);

        view.setOnClickListener(this);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        holder.itemView.setTag(position);
        holder.tv_name_script_item.setText(list.get(position).getUnickname());
        holder.tv_sum_script_item.setText(list.get(position).getScbrowse() + "");
        holder.tv_script_item.setText("\t\t" + list.get(position).getSccontent().substring(0,42) + "....");

    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        TextView tv_script_item;
        TextView tv_name_script_item;
        TextView tv_sum_script_item;

        ViewHolder(View view) {
            super(view);

            tv_script_item = view.findViewById(R.id.tv_script_item);
            tv_name_script_item = view.findViewById(R.id.tv_name_script_item);
            tv_sum_script_item = view.findViewById(R.id.tv_sum_script_item);

        }
    }
}
